set sourceDir [file dir [info script]]
#
package require Ttk
#~ package require hwat
#~ package require hwt
#
namespace eval ::weld {} {
	variable tabname "weldtool"
	variable nprefix "n"
	variable eprefix "e"
	variable width 12
}
# init
proc ::weld::init { } {
	variable tabname
	variable nprefix
	variable eprefix
	variable nullid
	variable width
	set tabname "weldtool"
	set nprefix "n"
	set eprefix "e"
	set nullid 0
	set width 12
}
# find components by config
proc ::weld::find_comp_by_config { configs } {
	eval *createmark components 1 {"by collected config"} $configs
	set allcomp [ hm_getmark components 1 ]
	*clearmark components 1
	return $allcomp
}

# find components by adjacent
proc ::weld::find_comp_by_adjacent { comp } {
	*clearmarkall 1
	*clearmarkall 2
	*createmark elems 1 "by comp id" $comp
	*createstringarray 2 "elements_on" "geometry_on"
	*isolateonlyentitybymark 1 1 2
	*createmark elems 1 "by config type" 1 quad4
	*appendmark elems 1 "by config type" 1 tria3
	
	if [ catch { *findmark elements 1 1 0 elements 0 2} ] {
	} else {	
		*createmark elems 1 "by config type" 1 quad4
		*appendmark elems 1 "by config type" 1 tria3
		
		*createmark elems 2 "by comp id" $comp
		*markdifference elems 1 elems 2
		if { [ llength [ hm_getmark elems 1 ] ] > 0 } { 
			*createstringarray 2 "elements_on" "geometry_on"
			*isolateonlyentitybymark 1 1 2
			*createmark components 1 "displayed"
			set allcomp [ hm_getmark components 1 ]
			return $allcomp
		}
	}
}
# create eset by nset
proc ::weld::creat_eset_by_nset { comp1 comp2 setname } {
	*clearmarkall 1
	*clearmarkall 2
	*createmark nodes 1 "by comp id" $comp
}

proc ::weld::min { a b } {
	return [ expr { ($a < $b) ? $a : $b} ]
}

##find node between two components  create node set 
proc ::weld::create_weldset_between_two { comp1 comp2 } {
	variable nprefix
	variable eprefix
	
	*clearmarkall 1
	*clearmarkall 2
	*createmark elems 1 "by comp id" $comp1 $comp2
	*createstringarray 2 "elements_on" "geometry_on"
	*isolateonlyentitybymark 1 1 2
	*createmark elems 1 "by config type" 1 quad4
	*appendmark elems 1 "by config type" 1 tria3
	*findbetween nodes elems 1 0 0 2
	
	set node_list [ hm_getmark nodes 2 ]
	
	if { [ llength $node_list ] > 0 } { 
		set t1 [ hm_getthickness comps $comp1]
		set t2 [ hm_getthickness comps $comp2]
		set tw [ ::weld::min $t1 $t2 ]
		set name1 "M_T$t1\_Z$tw\_$comp1:$comp2"
		set name2 "M_T$t2\_Z$tw\_$comp2:$comp1"
		if [ catch { eval *createentity sets type=nodes name="$nprefix\_$name1" ids={$node_list} } ] {
		} else {		
			eval *createmark elems 1 {"by node id"} $node_list
			*createstringarray 2 "elements_on" "geometry_on"
			*isolateonlyentitybymark 1 1 2
			*createmark elems 2 "by config type" 1 quad4
			*appendmark elems 2 "by config type" 1 tria3
			set elem_list_tmp [ hm_getmark elems 2 ]			
			*createmark elems 1 "by comp id" $comp1 $comp2
			*markdifference elems 2 elems 1
			eval *createmark elems 1 {"by id only"} $elem_list_tmp
			*markdifference elems 1 elems 2	
			
			set elem_list [ hm_getmark elems 1 ]			
			*createmark elems 2 "by comp id" $comp2
			*markdifference elems 1 elems 2
			catch { *entitysetcreate "$eprefix\_$name1" elems 1 }
			catch { eval *createentity sets type=nodes name="$nprefix\_$name2" ids={$node_list} }
			eval *createmark elems 2 {"by id only"} $elem_list
			*markdifference elems 2 elems 1
			catch { *entitysetcreate "$eprefix\_$name2" elems 2 }
			
			puts "create $comp1<-->$comp2"
		}
	}
}

proc ::weld::create_weldset_onebyone { comp1 comp2 } {
	variable nprefix
	variable eprefix
	
	*clearmarkall 1
	*clearmarkall 2
	*createmark elems 1 "by comp id" $comp1 $comp2
	*createstringarray 2 "elements_on" "geometry_on"
	*isolateonlyentitybymark 1 1 2
	*createmark elems 1 "by config type" 1 quad4
	*appendmark elems 1 "by config type" 1 tria3
	*findbetween nodes elems 1 0 0 2
	
	set node_list [ hm_getmark nodes 2 ]
	
	if { [ llength $node_list ] > 0 } { 
		set t1 [ hm_getthickness comps $comp1]
		set t2 [ hm_getthickness comps $comp2]
		set tw [ ::weld::min $t1 $t2 ]
		set name1 "M_T$t1\_Z$tw\_$comp1:$comp2"
		if [ catch { eval *createentity sets type=nodes name="$nprefix\_$name1" ids={$node_list} } ] {
		} else {		
			eval *createmark elems 1 {"by node id"} $node_list
			*createstringarray 2 "elements_on" "geometry_on"
			*isolateonlyentitybymark 1 1 2
			*createmark elems 2 "by config type" 1 quad4
			*appendmark elems 2 "by config type" 1 tria3
			set elem_list_tmp [ hm_getmark elems 2 ]			
			*createmark elems 1 "by comp id" $comp1 $comp2
			*markdifference elems 2 elems 1
			eval *createmark elems 1 {"by id only"} $elem_list_tmp
			*markdifference elems 1 elems 2	
			
			set elem_list [ hm_getmark elems 1 ]			
			*createmark elems 2 "by comp id" $comp2
			*markdifference elems 1 elems 2
			catch { *entitysetcreate "$eprefix\_$name1" elems 1 }
			
			puts "create $comp1 <--- $comp2"
		}
	}
}

##find all components
proc ::weld::create_nset_for_comps_attach { comps_list } {
	set n_comp [ llength $comps_list ]
	set i 1
	set comp1_ok ""
	# find node between components
	foreach comp1 $comps_list {
		puts "$i/$n_comp"
		puts "comp1 : $comp1"
		lappend comp1_ok $comp1
		*clearmarkall 1
		*clearmarkall 2
		
		eval *createmark comps 1  [ ::weld::find_comp_by_adjacent $comp1 ]
		eval *createmark comps 2  $comp1_ok
		
		*markdifference comps 1 comps 2
		set comp2_list [ hm_getmark comps 1 ]
		
		puts "comp2 : $comp2_list"
		foreach comp2 $comp2_list {
			puts "for comp1 : $comp1 ; comp2 : $comp2"
			::weld::create_weldset_between_two $comp1 $comp2
		}
		incr i
	}
}
# in selected components
proc ::weld::create_nset_for_comps_in { comps_list } {
	set n_comp [ llength $comps_list ]
	set i 1
	set comp1_ok ""
	# find node between components
	foreach comp1 $comps_list {
		puts "$i/$n_comp"
		puts "comp1 : $comp1"
		lappend comp1_ok $comp1
		*clearmarkall 1
		*clearmarkall 2
		
		set find_comp_list [ ::weld::find_comp_by_adjacent $comp1 ]
		eval *createmark comps 2  $find_comp_list
		eval *createmark comps 1  $comps_list
		*markdifference comps 2 comps 1
		
		eval *createmark comps 1  $find_comp_list		
		*markdifference comps 1 comps 2		
		eval *createmark comps 2  $comp1_ok		
		*markdifference comps 1 comps 2
		
		set comp2_list [ hm_getmark comps 1 ]
		
		puts "comp2 : $comp2_list"
		foreach comp2 $comp2_list {
			puts "for comp1 : $comp1 ; comp2 : $comp2"
			::weld::create_weldset_between_two $comp1 $comp2
		}
		incr i
	}
}

proc ::weld::create_nset_for_config { config } {
	::weld::create_nset_for_comps_attach [ find_comp_by_config $config ]
}
########################################################################################
# out puts
#~ * obj:  1--node  2--elem  3--set
proc ::weld::get_weld { prefix obj res1 prefix2 fem } {
	#~ ==============================================
	variable weldid
	#~ ==============================================
	if { [ string equal $obj "1" ] } {
		set entity nodes 
	} elseif  { [ string equal $obj "2" ] } {
		set entity elements
	}
	#~ ==============================================
	set proptype sets

	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1
	foreach propid $myprop {
		
		set name [ hm_getvalue sets id=$propid dataname=name ]
		set type [ hm_getvalue sets id=$propid dataname=type ]
		
		set p [ string first $prefix $name ]
		if  { [ string equal $type $obj ] && [ string equal $p "0" ] } {		
			set name2 [ string replace $name 0 [ expr { [string length $prefix ] -1 } ] $prefix2 ]
			set token [ split $name "_" ]
			if { [ llength $token ] <5 } {
				puts "$entity set '$name' with wrong name"
				continue
			}
			set token_1 [ lindex $token 1 ]
			if { [ string match N* $token_1 ] } {
				set band "no"
			} else {
				set band "true"
			}
			set token_2 [ lindex $token 2 ]
			set tb [ string map {p . T "" " " "" "\n" ""} $token_2 ]
			set token_3 [ lindex $token 3 ]
			if { [ string match Z* $token_3 ] } {
				set weld_type C
				set tz [ string map { Z "" p .} $token_3 ] 
				set tw [ expr $tz/sqrt(2) ]
			} elseif { [ string match H* $token_3 ] } {
				if { [ string match *+* $token_3 ] } {
					set weld_type C
					set token [ split $token_3 "+" ]
					set tk0 [ lindex $token 0 ] 
					set tk1 [ lindex $token 1 ] 
					set th [ string map {H "" " " "" "\n" ""} $tk0 ]
					set tz [ string map {Z "" " " "" "\n" ""} $tk1 ]
					set tw [ expr $th+$tz/sqrt(2) ]
				} else {
					set weld_type C
					set tw [ string map { H "" p . } $token_3 ] 
				}
			} elseif { [ string match DH* $token_3 ] } {
				set weld_type B
				set tw [ string map { H "" D "" p . } $token_3 ] 
			} else {
				puts "$entity set '$name' with wrong weld_type"
				set weld_type C
				set tw [ expr $tb/sqrt(2) ]
			}
			
			if [ hm_entityinfo exist sets $name2 ] {
				if { [ string equal $fem weldfiller ] } {
					puts $res1 "$weldid,\t$name2,\t$name,\t$weld_type,\t$tb,\t$tw,\t1,\t$band"
					incr weldid
				} elseif { [ string equal $fem weldforce ] } {
					puts $res1 "$weldid,\t$name2,\t$name,\t$tw,\t1"
					incr weldid
				}
			} 
		}
	}
}

#~ ==============================================
#~ * obj:  1--node  2--elem  3--set
proc ::weld::get_set { prefix obj res1 } {
	#~ ==============================================
	if { [ string equal $obj "1" ] } {
		set entity nodes 
	} elseif  { [ string equal $obj "2" ] } {
		set entity elements
	}
	#~ ==============================================
	set proptype sets

	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1

	foreach propid $myprop {
		
		set org_name [ hm_getvalue sets id=$propid dataname=name ]		
		set type [ hm_getvalue sets id=$propid dataname=type ]
		
		set name [ string map {" " "" "\n" "" "\t" ""} $org_name ]
		set p [ string first $prefix $name ]
		
		if  { [ string equal $type $obj ] && [ string equal $p "0" ] } {		
			set myelems [ hm_getentityarray $proptype $propid $entity ]
			set elem_num [ llength $myelems ]
			puts $res1 "$propid,$name,$elem_num\n$myelems"
		}
	}
}


#~ ==================================================================
proc ::weld::write_out { fem } { 
	variable weldid
	variable nprefix
	variable eprefix
	set Out_path [tk_chooseDirectory -title "select Output path..."]
	
	if { $Out_path == "" } { return }
	#~ ==================================================
	#~ weld 
	if [ catch { open "$Out_path/weldfile" w} res1 ] {
		puts "Cannot open weldfile for write:$res1"
	}
	
	set weldid 1
	::weld::get_weld $nprefix\_ 1 $res1 $eprefix\_ $fem
	close $res1
	puts   "output : $Out_path/weldfile"
	#~ ==================================================
	#~ eset 
	if [ catch { open "$Out_path/esetfile" w} res1 ] {
		puts "Cannot open esetfile for write:$res1"
	}

	::weld::get_set $eprefix\_ 2 $res1
	close $res1
	puts   "output : $Out_path/esetfile"
	#~ ==================================================
	#~ nset 
	if [ catch { open "$Out_path/nsetfile" w} res1 ] {
		puts "Cannot open nsetfile for write:$res1"
	}

	::weld::get_set $nprefix\_ 1 $res1
	close $res1
	puts   "output : $Out_path/nsetfile"
}
########################################################################################
# find all components
proc ::weld::create_nset_for_select_attach { } {
	*createmarkpanel comps 1 "Please Select Components."
	::weld::create_nset_for_comps_attach [hm_getmark comps 1]
}

proc ::weld::create_nset_for_select_in { } {
	*createmarkpanel comps 1 "Please Select Components."
	::weld::create_nset_for_comps_in [hm_getmark comps 1]
}

# -----------------------------------------------------------------

proc ::weld::create_nset_between_old { } {
	*createmarkpanel comps 1 "Please Select 2 Components."
    set comps_list [hm_getmark comps 1]
	if { [ llength $comps_list ] == 2 } {
		eval ::weld::create_weldset_between_two $comps_list
	}
}

# -----------------------------------------------------------------------

proc ::weld::set_nprefix { } {
	variable nprefix
	set newprefix [ hm_getstring "new nset prefix" ]
	if { $newprefix == "" } {
	} else {
		set nprefix $newprefix
	}
}

proc ::weld::set_eprefix { } {
	variable eprefix
	set newprefix [ hm_getstring "new eset prefix" ]
	if { $newprefix == "" } {
	} else {
		set eprefix $newprefix
	}
}

#==========================================================
# rename
proc ::weld::change_name_done { proptype } {
	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1

	foreach propid $myprop {
		set propname [ hm_getcollectorname $proptype $propid ]
		puts "$proptype : ( $propid , $propname )"
		set newpropname [ string map {. p " " "" "\n" ""} $propname ]
		if { [ string match {[0-9]*} $newpropname ] } {
			set newpropname "T$newpropname"
		}
		if { [ hm_entityinfo exist $proptype $newpropname ] } {
			set newpropname [ hm_getincrementalname $proptype $newpropname 1 ] 
		}
		if {![string equal $propname $newpropname]} {
			*renamecollector $proptype $propname $newpropname
		}
	}
}

proc ::weld::change_name { } { 
	::weld::change_name_done components
	#~ ::weld::change_name_done properties
	::weld::change_name_done materials
	::weld::change_name_done sets
	::weld::change_name_done loadsteps
	::weld::change_name_done loadcols
	::weld::change_name_done assemblies
	::weld::change_name_done beamsects
}

#============================================================================
#create property
proc ::weld::create_abaqus_mat { matname {dens 7.8e-9} {ex 206000} {nuxy 0.3} } {
	*createentity mats cardimage=ABAQUS_MATERIAL name=$matname
	*setvalue mats name=$matname STATUS=2 182=1
	*setvalue mats name=$matname STATUS=2 5003=0
	*setvalue mats name=$matname STATUS=2 33=0
	*setvalue mats name=$matname STATUS=0 184=1
	*setvalue mats name=$matname STATUS=2 183={$dens}
	*setvalue mats name=$matname STATUS=2 5={0}
		
	*setvalue mats name=$matname STATUS=2 104=1
	*setvalue mats name=$matname STATUS=2 36=0
	*setvalue mats name=$matname STATUS=2 825=0
	*setvalue mats name=$matname STATUS=2 105=1
	*setvalue mats name=$matname STATUS=0 26=1
	*setvalue mats name=$matname STATUS=2 3={$ex}
	*setvalue mats name=$matname STATUS=2 4={$nuxy}
	*setvalue mats name=$matname STATUS=2 71={0}
	*setvalue mats name=$matname STATUS=2 2973=0
	*setvalue mats name=$matname STATUS=2 2975=0
}

proc ::weld::create_abaqus_shell_prop {  name mat t } {
	*createentity props cardimage=SHELLSECTION name=$name
	*setvalue props name=$name materialid={mats [hm_getvalue mats name=$mat dataname=id]}
	*setvalue props name=$name STATUS=1 111=$t
}

proc ::weld::create_prop_abaqus { } {
	*createmark components 1 all
	set mycomps [ hm_getmark components 1 ]
	*clearmark components 1
	
	foreach compid $mycomps {
		set compname [ hm_getcollectorname components $compid ]
		set token [ split $compname "_" ]
		set n [ llength $token ]
		if { $n < 2 } continue
		
		set thick [ lindex $token [expr $n-1 ] ]
		if { [ string match {*mm} $thick ] } {
		} else {
			continue
		}
		if { [regexp {[0-9p]+} $thick ans] } { 
		} else {
			continue
		}
		set t [ string map { p . } $ans ]
		
		set mat [ lindex $token [expr $n-2 ] ] 
		set matname "mat_$mat"
		if { [hm_entityinfo exist materials $matname] } {
		} else {
			::weld::create_abaqus_mat $matname
		}
		set pr_name [ format "pr_%s_%smm" $mat $ans ]
		
		if { [hm_entityinfo exist properties $pr_name] } {
		} else {
			::weld::create_abaqus_shell_prop $pr_name $matname $t
		}
		
		*setvalue comps id=$compid propertyid={props [hm_getvalue props name=$pr_name dataname=id]}
		
		puts "$compname : ( id=$compid , prop=$pr_name , mat=$matname , t=$t )"
	}	
}
#============================================================================
# read ansys mat for abaqus
proc ::weld::read_ansys_mat_for_abaqus { } { 
	if [ catch { open mat.txt w} res ] {
		puts "Cannot open mat.txt for write:$res"
		return
	}

	#~ =======================================
	set proptype materials

	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1

	foreach propid $myprop {
		puts "materials--$propid"
		set cardimage [ hm_getvalue mats id=$propid dataname=cardimage ] 
		if { [ string equal $cardimage "MATERIAL" ] } {
			set dens [ hm_getvalue mats id=$propid dataname=2619 ]
			set ex [ hm_getvalue mats id=$propid dataname=2645 ]
			set nuxy [ hm_getvalue mats id=$propid dataname=2608 ]
		} elseif { [ string equal $cardimage "MPDATA" ] } {
			set dens [ hm_getvalue mats id=$propid dataname=512 ]
			set ex [ hm_getvalue mats id=$propid dataname=516 ]
			set nuxy [ hm_getvalue mats id=$propid dataname=608 ]
		}
		puts $res "$dens $ex $nuxy"
	}

	# =======================================
	set proptype components

	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1

	foreach propid $myprop {
		puts "components--$propid"
		set name [ hm_getvalue comps id=$propid dataname=name ]
		set sensorid [ hm_getvalue comps id=$propid dataname=3081 ]
		set propertyid [ hm_getvalue comps id=$propid dataname=propertyid ]
		set materialid [ hm_getvalue comps id=$propid dataname=materialid ]
		
		if { [ string equal $sensorid "0" ] } {		
			set cardimage "unsupported"
		} else {
			set cardimage [ hm_getvalue sensors id=$sensorid dataname=cardimage ]
		}
		
		if { [ string equal $cardimage "SHELL181" ] } {
			set thickness [ hm_getvalue props id=$propertyid dataname=136 ]
			puts $res "$name $materialid $cardimage $thickness"
		} elseif { [ string equal $cardimage "MASS21" ] } {
			set mass [ hm_getvalue props id=$propertyid dataname=136 ]
			puts $res "$name $materialid $cardimage $mass"
		} elseif { [ string equal $cardimage "BEAM188" ] } {
			set secid [ hm_getvalue props id=$propertyid dataname=3007 ]
			if { $secid == 0 } {
				set r [  hm_getvalue props id=$propertyid dataname=2782 ]
			} else {
				set r [ hm_getvalue beamsects id=$secid dataname=beamsect_dim1 ]
			}
			puts $res "$name $materialid $cardimage $r"
		} elseif { [ string equal $cardimage "SOLID185" ] } {
			puts $res "$name $materialid $cardimage"
		} else {
			puts $res "$name $materialid unsupported"
		}
	}

	# ==================================================================
	close $res

}
#============================================================================
# write abaqus mat
proc ::weld::write_abaqus_mat { } {

	if [ catch { open mat.txt r} res ] {
		puts "Cannot open mat.txt for read:$res"
		return
	}

	# =================================================
	set proptype materials

	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1

	foreach propid $myprop {
		gets $res line
		set data [ split $line " " ]
		set dens [ lindex $data 0 ]
		set ex [ lindex $data 1 ]
		set nuxy [ lindex $data 2 ]
		
		*setvalue mats id=$propid cardimage="ABAQUS_MATERIAL"
		*setvalue mats id=$propid STATUS=2 182=1
		*setvalue mats id=$propid STATUS=2 5003=0
		*setvalue mats id=$propid STATUS=2 33=0
		*setvalue mats id=$propid STATUS=0 184=1
		*setvalue mats id=$propid STATUS=2 183={$dens}
		*setvalue mats id=$propid STATUS=2 5={0}
		
		*setvalue mats id=$propid STATUS=2 104=1
		*setvalue mats id=$propid STATUS=2 36=0
		*setvalue mats id=$propid STATUS=2 825=0
		*setvalue mats id=$propid STATUS=2 105=1
		*setvalue mats id=$propid STATUS=0 26=1
		*setvalue mats id=$propid STATUS=2 3={$ex}
		*setvalue mats id=$propid STATUS=2 4={$nuxy}
		*setvalue mats id=$propid STATUS=2 71={0}
		*setvalue mats id=$propid STATUS=2 2973=0
		*setvalue mats id=$propid STATUS=2 2975=0
	}
	#==================================
	set proptype properties

	*createmark $proptype 1 all
	*deletemark $proptype 1
	*clearmark $proptype 1
	
	#==================================
	set proptype components

	*createmark $proptype 1 all
	set myprop [ hm_getmark $proptype 1 ]
	*clearmark $proptype 1

	foreach propid $myprop {
		gets $res line
		set data [ split $line " " ]
		set name [ lindex $data 0 ]
		set materialid [ lindex $data 1 ]
		set cardimage [ lindex $data 2 ]
		
		if { [ string equal $cardimage "SHELL181" ] } {
			set thickness [ lindex $data 3 ]
			catch { *createentity props cardimage=SHELLSECTION name="pr_$name" }
			set propertyid [ hm_getvalue props name="pr_$name" dataname=id ]
			*setvalue props id=$propertyid materialid={mats $materialid}
			*setvalue props id=$propertyid STATUS=1 111=$thickness		
			*setvalue comps id=$propid propertyid={props $propertyid}
		} elseif { [ string equal $cardimage "MASS21" ] } {
			set mass [ lindex $data 3 ]
			catch { *createentity props cardimage=MASS name="pr_$name" }
			set propertyid [ hm_getvalue props name="pr_$name" dataname=id ]
			*setvalue props id=$propertyid STATUS=1 461=$mass		
			*setvalue comps id=$propid propertyid={props $propertyid}
		} elseif { [ string equal $cardimage "BEAM188" ] } {
			set r [ lindex $data 3 ]
			catch { *createentity props cardimage=BEAMSECTION name="pr_$name" }
			set propertyid [ hm_getvalue props name="pr_$name" dataname=id ]
			*setvalue props id=$propertyid materialid={mats $materialid}
			*setvalue props id=$propertyid STATUS=2 234=2
			*setvalue props id=$propertyid STATUS=2 246=$r
			*setvalue props id=$propertyid STATUS=2 688=1
			*setvalue props id=$propertyid STATUS=1 685=1
			*setvalue props id=$propertyid STATUS=1 686=1
			*setvalue props id=$propertyid STATUS=1 687=1
			set propertyid [ hm_getvalue props name="pr_$name" dataname=id ]
			*setvalue comps id=$propid propertyid={props $propertyid}
		} elseif { [ string equal $cardimage "SOLID185" ] } {
			catch { *createentity props cardimage=SOLIDSECTION name="pr_$name" }
			set propertyid [ hm_getvalue props name="pr_$name" dataname=id ]
			*setvalue props id=$propertyid materialid={mats $materialid}		
			*setvalue comps id=$propid propertyid={props $propertyid}
		} else {
			puts "$name $propid $materialid  $cardimage"
		}
	}
	
	# ===============================================
	close $res
	# ===============================================
	*elementtype 1 1
	*elementtype 21 1
	*elementtype 60 9
	*elementtype 63 1
	*elementtype 61 5
	*elementtype 70 1
	*elementtype 103 1
	*elementtype 108 42
	*elementtype 104 7
	*elementtype 106 1
	*elementtype 204 1
	*elementtype 206 1
	*elementtype 208 7
	*elementtype 210 3
	*elementtype 215 1
	*elementtype 220 3
	*elementtype 205 7
	*elementtype 213 3
	*elementtype 5 9
	*elementtype 56 2
	*elementtype 2 1
	*elementtype 3 1
	*elementtype 23 1
	*elementtype 24 1
	*elementtype 57 1
	*elementtype 108 1
	*createmark elements 1 "all"
	*elementsettypes 1
	*clearmark elements 1
}


#============================================================================
# GUI

proc ::weld::GUIlabel { frm labelname } {
	set frmlable [frame $frm.frm$labelname]
	pack $frmlable -side top -anchor nw 
	set label [label $frmlable.$labelname -text "$labelname"]
	pack $label -side left -anchor nw
}

proc ::weld::GUIvariable { frm varlabel text cmd var } {
	variable width
	set frm_var [frame $frm.frm_$varlabel]
	pack $frm_var -side top -anchor nw 
	set btn   [button $frm_var.btn_$varlabel  -text $text -width $width -command  $cmd ]
	set label [label $frm_var.label_$varlabel -textvariable $var  -width $width]
	pack $btn $label -side left -anchor nw
}

proc ::weld::GUInullspace { frm } {
	variable nullid
	set null [label $frm.null$nullid -text " "]
	pack $null  -side top -anchor nw
	incr nullid
}
#=============================================================
proc ::weld::select_comp { var } {
	variable $var
	set $var [ hm_callcollectorpanel comps ]
}
#=============================================================
proc ::weld::GUISelectComp { frm varlabel text cmd var } {
	variable width
	set frm_var [frame $frm.frm_$varlabel]
	pack $frm_var -side top -fill x 
	set btn   [button $frm_var.btn_$varlabel  -text $text -width [expr 2*$width] -command $cmd ]
	set ent   [entry $frm_var.entry_$varlabel -textvariable $var -bd 2 ]
	pack $btn -side left 
	pack $ent -side left -fill x -expand 1
}

proc ::weld::create_nset_onebyone_done { } {
	variable m_comp_A
	variable m_comp_B
	set id_A [ hm_getvalue comps name=$m_comp_A dataname=id ]
	set id_B [ hm_getvalue comps name=$m_comp_B dataname=id ]
	::weld::create_weldset_onebyone $id_A $id_B 
}
proc ::weld::create_nset_onebyone { } {
	variable m_onebyone_panal
	variable m_comp_A ""
	variable m_comp_B ""
	variable width
	
	catch {destroy $m_onebyone_panal}
	set m_onebyone_panal [frame .g_onebyone_panal -padx 7 -pady 7]
	
	set top [frame $m_onebyone_panal.top  ]
	pack $top -side top -fill x
	set bottom [frame $m_onebyone_panal.bottom  ]
	pack $bottom -side bottom -fill x -expand 0
	
	set right [ frame $top.right  ]
	pack $right -side right -fill y
	button $right.create -text "create" -command {::weld::create_nset_onebyone_done} -bg #339933 -width $width 
	pack $right.create -side top -fill x
	
	set left [ frame $top.left  ]
	pack $left -side left -expand 1 -fill y -fill x
	::weld::GUISelectComp $left "setcompA" "weld component: " {::weld::select_comp ::weld::m_comp_A } ::weld::m_comp_A
	::weld::GUISelectComp $left "setcompB" "attached comp : " {::weld::select_comp ::weld::m_comp_B } ::weld::m_comp_B	

	button $bottom.button -text "return" -width $width -command hm_exitpanel -bg #C06060
	pack $bottom.button -side right -anchor e;

	hm_framework addpanel $m_onebyone_panal "Create weld one by one";
	hm_framework drawpanel $m_onebyone_panal;
}

#=============================================================
proc ::weld::weldtool_GUI { mymain } {
	variable nprefix
	variable eprefix
	variable width
	
	set weldtool [labelframe $mymain.weldtool -text "Weld tools" ]
	pack $weldtool -side top -fill x -anchor nw
	#  set nset  prefix
	::weld::GUIlabel    $weldtool "variables:"
	::weld::GUIvariable $weldtool "setnprefix" "nset prefix : " ::weld::set_nprefix ::weld::nprefix
	::weld::GUIvariable $weldtool "seteprefix" "eset prefix : " ::weld::set_eprefix ::weld::eprefix

	::weld::GUInullspace $weldtool
	#method:
	::weld::GUIlabel $weldtool "method:"
	
	set frmtool [frame $weldtool.frmtool]
	pack $frmtool -side top -anchor nw 
	set btn_one     [button $frmtool.btn_one    -text "one by one"  -width $width -command {::weld::create_nset_onebyone}]
	set btn_in_sel  [button $frmtool.btn_in_sel -text "in selected" -width $width -command {::weld::create_nset_for_select_in}]
	set btn_attach  [button $frmtool.btn_attach -text "attach to"   -width $width -command {::weld::create_nset_for_select_attach}]
	set btn_all     [button $frmtool.btn_all    -text "all shell"   -width $width -command {::weld::create_nset_for_config  "quad4"}]
	grid $btn_one $btn_in_sel
	grid $btn_attach $btn_all
	
	# out put
	::weld::GUInullspace $weldtool
	::weld::GUIlabel $weldtool "outputs:"
	set frmoutput [frame $weldtool.frmoutput]
	pack $frmoutput -side top -anchor nw 
	set btn_weldfiller [button $frmoutput.btn_weldfiller  -text "for weldfiller"  -width $width -command {::weld::write_out weldfiller}]
	set btn_weldforce  [button $frmoutput.btn_weldforce   -text "for weldforce"   -width $width -command {::weld::write_out weldforce}]
	grid $btn_weldfiller $btn_weldforce
}
#=============================================================
proc ::weld::exit_GUI { mymain } {
	variable width
	set frmexit [frame $mymain.frmexit]
	pack $frmexit -side top -fill x -anchor nw	
	set btn_closeSF [button $frmexit.btn_closeSF  -text "exit" -width $width -command {hm_framework removetab $::weld::tabname}] 
	pack $btn_closeSF -side right -anchor nw
}
#=============================================================
proc ::weld::common_GUI { mymain } {
	variable width
	set commontool [labelframe $mymain.commontool -text "Common tools" ]
	pack $commontool -side top -fill x -anchor nw
	set change_name   [button $commontool.change_name  -text "change name"  -width $width -command {::weld::change_name} ]
	set create_prop_abaqus   [button $commontool.create_prop_abaqus  -text "create prop abaqus"  -width [expr 2*$width+1] -command {::weld::create_prop_abaqus} ]
	grid $change_name x -sticky nw
	grid $create_prop_abaqus - -sticky nw
}
#=============================================================
proc ::weld::ansys2abaqus_GUI { mymain } {
	variable width
	set ansys2abaqus [labelframe $mymain.ansys2abaqus -text "ANSYS -> ABAQUS" ]
	pack $ansys2abaqus -side top -fill x -anchor nw
	set writeansysmat  [button $ansys2abaqus.writeansysmat   -text "output ansys"   -width $width -command {::weld::read_ansys_mat_for_abaqus}]
	set readabaqusmat  [button $ansys2abaqus.readabaqusmat   -text "input abaqus"   -width $width -command {::weld::write_abaqus_mat}]
	grid $writeansysmat $readabaqusmat
}

#=============================================================
proc ::weld::ansys2pamcrash_GUI { mymain } {
	variable width
	set ansys2pamcras [labelframe $mymain.ansys2pamcras -text "ANSYS -> PAMCRASH" ]
	pack $ansys2pamcras -side top -fill x -anchor nw
	set writeansysmat    [button $ansys2pamcras.writeansysmat    -text "output ansys"    -width $width -command {::weld::write_out weldforce}]
	set readpamcrsashmat [button $ansys2pamcras.readpamcrsashmat -text "input pamcrash"  -width $width -command {::weld::write_out weldforce}]
	grid $writeansysmat $readpamcrsashmat
}

#=============================================================
proc ::weld::create_GUI {} {
	variable tabname
	# init
	::weld::init
	
	# removetab first
	catch {hm_framework removetab $tabname}	
	catch {	destroy .dtMainFrame }
	
	# main frame
	set dtMainFrame [frame .dtMainFrame]
	pack $dtMainFrame -side top -fill both -expand 1	 
	
	set mymain [frame $dtMainFrame.main]
	pack $mymain -side bottom -fill x -anchor nw
	
	#common tools
	::weld::common_GUI $mymain
	::weld::GUInullspace $mymain
	
	# ansys to abaqus
	::weld::ansys2abaqus_GUI $mymain
	::weld::GUInullspace $mymain
	
	#ansys to pamcrash
	::weld::ansys2pamcrash_GUI $mymain
	::weld::GUInullspace $mymain
	
	# weld frame
	::weld::weldtool_GUI $mymain
	
	#exit:
	::weld::exit_GUI $mymain
	
	##the following code for hypermesh tab
	hm_framework addtab $tabname $dtMainFrame
}

#==============================================================================================
# main 

::weld::create_GUI
